from flask import render_template, request, jsonify, session, make_response, redirect
from werkzeug.security import generate_password_hash, check_password_hash

from views import registered_blue
from models.index import User, db


# 注册页面
@registered_blue.route('/registered/registered')
def registered():
    return render_template('Registered.html')


# 注册页面逻辑
@registered_blue.route('/registered/form', methods=['GET', 'POST'])
def registered_form():
    # 获取注册表中的数据
    mobile = request.json.get('mobile')  # 电话或电子邮箱
    password = request.json.get('password_one')  # 密码
    password_two = request.json.get('password_two')  # 确认密码
    image_code = request.json.get('image_code')  # 图片验证码
    print('-----mobile------', mobile)
    print('-----password------', password)
    print('----password_two-------', password_two)
    print('-----image_code------', image_code)

    image_code_text = session.get('image_code_text')

    ret = '注册失败'
    ret = {
        "errno": 1000,
        "errmsg": "注册成功"
    }
    # 表单未填写完成
    if not (mobile and password and password_two and image_code):
        ret = {
            "errno": 1004,
            "errmsg": "表单未填写完成"
        }
    else:

        # 电话号码没有11位,11位为纯数字
        print('mobile.count()---------', len(mobile), mobile.isdigit())
        if len(mobile) != 11 and mobile.isdigit():
            ret = {
                "errno": 1001,
                "errmsg": "电话号码错误"
            }

        # 判断输入的两次密码是否一致
        elif password != password_two:
            ret = {
                "errno": 1002,
                "errmsg": "两次密码输入不一致"
            }

        # 判断输入的验证码是否正确
        elif image_code_text.upper() != image_code.upper():
            ret = {
                "errno": 1003,
                "errmsg": "验证码错误"
            }

        # 将数据存入数据库
        else:

            # 判断是否已经注册过账号
            if_user = db.session.query(User).filter(User.mobile == mobile).first()
            if if_user:
                session['user_id'] = if_user.id
                session['mobile'] = if_user.mobile
                ret = {
                    "errno": 1000,
                    "errmsg": "已经注册过账号"
                }
                return jsonify(ret)
            # 没有注册过账号存入数据
            user = User()
            user.mobile = mobile
            user.password = generate_password_hash(password)
            user.nick_name = mobile
            user.gender = '保密'
            user.img_url = '/static/images/login_wrong.png'
            db.session.add(user)
            db.session.commit()

            # user对象存入session
            session['user_id'] = user.id
            session['mobile'] = user.mobile

            ret = {
                "errno": 1000,
                "errmsg": "注册成功"
            }

    print(ret)
    return jsonify(ret)


# 图片验证码
@registered_blue.route('/registered/image_code')
def img_code():
    from utils.captcha.captcha import captcha
    name, text, image = captcha.generate_captcha()
    session['image_code_text'] = text
    print('--------text------->', text)
    # print('--------name------->', name)
    resp = make_response(image)
    resp.headers['Content-Type'] = 'image/png'
    # print('-----resp---------', resp)
    return resp


# 登录页面
@registered_blue.route('/registered/login')
def login():
    return render_template('Login.html')


# 登录逻辑
@registered_blue.route('/registered/login_form', methods=['POST'])
def login_form():
    # 获取数据和打印
    mobile = request.json.get('mobile')
    password = request.json.get('password')
    print(mobile, password)

    user = db.session.query(User).filter(User.mobile == mobile).first()
    print('------------user-----------', user)
    if password and user:
        if not check_password_hash(user.password, password):
            ret = {
                "errno": 2001,
                "errmsg": "账号密码错误"
            }
        else:
            ret = {
                "errno": 2000,
                "errmsg": "登录成功"
            }
            session['user_id'] = user.id
            session['mobile'] = mobile
    else:
        ret = {
            "errno": 2001,
            "errmsg": "账号密码错误"
        }
    return jsonify(ret)


# 退出登录
@registered_blue.route('/registered/quit')
def registered_quit():
    # 清除session缓存
    session.clear()
    # 跳转到主页
    return redirect('/')
